package DataBase;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import common.Tables.Psychologists;
import common.Tables.Table;
import common.Tables.Tasks;
import common.Tables.TasksNotes;

/**
 * class DBTaskPerPsych.
 * in charge of adding, deleting and changing TaskPerPsych objects 
 * @author michal
 *
 */
public class DBTaskPerPsych extends DataBase{

	/**
	 * c-tor
	 * @param sessionFactory
	 * @param session
	 */
	public DBTaskPerPsych(SessionFactory sessionFactory, Session session){
		this.sessionFactory=sessionFactory;
		this.session = session;
	}

	/**
	 * gets a psych is and a task id and removes it from TaskPerPsych table
	 * @param psychID
	 * @param taskIdOld
	 */
	public void delete(String psychID, String taskIdOld) {
		DBPsychologists dbPsychologists = new DBPsychologists(sessionFactory, session);
		Psychologists psych = (Psychologists) dbPsychologists.getPsychsByID(psychID).get(0);
		Iterator<TasksNotes> it = psych.getTasksNotes().iterator();
		TasksNotes tn;
		while (it.hasNext()){	//iterating the tasksNotes set
			tn = it.next();
			if (tn.getTaskID().getId()==Integer.valueOf(taskIdOld)){	//task ids matches
				//removing the task from the psych's set
				psych.removeTask(tn.getTaskID());
				session.delete(tn);
				session.update(psych);
				break;
			}
		}		
		session.flush();
	}
	
	/**
	 * method gets a task id and removes all associations with this task in the
	 * TaskPerPsych table
	 * @param taskID
	 */
	public void deleteAssociationWithTask(int taskID){
		Transaction txn = session.getTransaction();
		txn.begin();
		Query q;
		//getting all rows with this task id
		String queryStart2= "from TasksNotes where TASK_ID='"+taskID+"'";
		List<Table> ans = generalQuery(queryStart2);
		//removing all rows with TASKS_NOTES_ID (which are associated with the given task id) 
		String queryStart1="DELETE from TaskPerPsych where TASKS_NOTES_ID IN ( ";
		if (!ans.isEmpty()){
			for (Table tn : ans){//completing the query
				queryStart1+="'"+((TasksNotes)tn).getTasksNotesID()+"',";
			}
			queryStart1 = queryStart1.substring(0, queryStart1.length()-1);
			queryStart1+=")";
			q= session.createQuery(queryStart1);
			//Executing second query
			q.executeUpdate();
		}
		//executing first query
		q= session.createQuery("DELETE "+queryStart2);
		q.executeUpdate();
		session.flush();	
		if (!txn.wasCommitted())
			txn.commit();
	}

	/**
	 * method gets a list of old info and removes it from DB
	 * @param oldRows
	 */
	public void delete(ArrayList<ArrayList<String>> oldRows) {
		Transaction txn = session.getTransaction();
		txn.begin();
		String psychID;
		String taskIdOld = null;
		for (ArrayList<String> row : oldRows){
			psychID = row.get(0);
			taskIdOld = row.get(1);
			delete(psychID,taskIdOld);
		}
		if (!txn.wasCommitted())
			txn.commit();
	}
}
